//
//  GTNormalTableViewCell.m
//  SampleApp2
//
//  Created by GXHC on 2023/6/9.
//

#import "GTNormalTableViewCell.h"
#import "../Model/GTListItem.h"
#import <SDWebImage.h>
#import "../../Utils/GTScreen.h"
@interface GTNormalTableViewCell ()
@property (nonatomic, strong, readwrite) UILabel *titleLabel;
@property (nonatomic, strong, readwrite) UILabel *sourceLabel;
@property (nonatomic, strong, readwrite) UILabel *commentLabel;
@property (nonatomic, strong, readwrite) UILabel *timeLabel;
@property (nonatomic, strong, readwrite) UIImageView *rightImageView;
@property (nonatomic, strong, readwrite) UIButton *deleteButton;
@end

@implementation GTNormalTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {
        [self.contentView addSubview:({
            self.titleLabel = [[UILabel alloc]initWithFrame:UIRect(20, 15, 260, 50)];

            self.titleLabel.font = [UIFont systemFontOfSize:16];
            self.titleLabel.textColor = [UIColor blackColor];
			self.titleLabel.numberOfLines = 2;
			self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
            self.titleLabel;
        })];
        [self.contentView addSubview:({
            self.sourceLabel = [[UILabel alloc]initWithFrame:UIRect(20, 70, 50, 20)];

            self.sourceLabel.font = [UIFont systemFontOfSize:12];
            self.sourceLabel.textColor = [UIColor grayColor];
            self.sourceLabel;
        })];
        [self.contentView addSubview:({
            self.commentLabel = [[UILabel alloc]initWithFrame:UIRect(100, 70, 50, 20)];

            self.commentLabel.font = [UIFont systemFontOfSize:12];
            self.commentLabel.textColor = [UIColor grayColor];
            self.commentLabel;
        })];
        [self.contentView addSubview:({
            self.timeLabel = [[UILabel alloc]initWithFrame:UIRect(150, 70, 50, 20)];

            self.timeLabel.font = [UIFont systemFontOfSize:12];
            self.timeLabel.textColor = [UIColor grayColor];
            self.timeLabel;
        })];

        [self.contentView addSubview:({
            self.rightImageView = [[UIImageView alloc] initWithFrame:UIRect(320, 15, 70, 70)];
//            self.rightImageView.backgroundColor = [UIColor redColor];
            self.rightImageView.contentMode = UIViewContentModeScaleToFill;
            self.rightImageView;
        })];
		
		[self.contentView addSubview:({
			self.deleteButton = [[UIButton alloc] initWithFrame:UIRect(270, 70, 30, 20)];
			[self.deleteButton setTitle:@"x" forState:UIControlStateNormal];
			[self.deleteButton setTitle:@"v" forState:UIControlStateHighlighted];
			[self.deleteButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
			[self.deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
//			self.deleteButton.backgroundColor = [UIColor blueColor];
			self.deleteButton.layer.cornerRadius = 10;
			self.deleteButton.layer.masksToBounds= YES;
			self.deleteButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
			self.deleteButton.layer.borderWidth = 2;
			self.deleteButton;
		})];
    }

    return self;
}

- (void) deleteButtonClick{
	NSLog(@"");
	if(self.delegate && [self.delegate respondsToSelector:@selector(tableViewCell:clickDeleteButton:)]){
		[self.delegate tableViewCell:self clickDeleteButton:self.deleteButton];
	}
}

- (void)layoutTableViewCellWithItem:(GTListItem *)item {
	
	BOOL hasRead = [[NSUserDefaults standardUserDefaults]boolForKey:item.uniquekey];
	if(hasRead){
		self.titleLabel.textColor = [UIColor lightGrayColor];
	}else{
		self.titleLabel.textColor = [UIColor blackColor];
	}
	
    self.titleLabel.text =item.title;

    self.sourceLabel.text = item.authorName;
    [self.sourceLabel sizeToFit];

    self.commentLabel.text =item.category;
    [self.commentLabel sizeToFit];
    self.commentLabel.frame = CGRectMake(self.sourceLabel.frame.origin.x + self.sourceLabel.bounds.size.width + 15, self.commentLabel.frame.origin.y, self.commentLabel.frame.size.width, self.commentLabel.frame.size.height);

    self.timeLabel.text = item.date;
    [self.timeLabel sizeToFit];
    self.timeLabel.frame = CGRectMake(self.commentLabel.frame.origin.x + self.commentLabel.frame.size.width + 15, self.timeLabel.frame.origin.y, self.timeLabel.frame.size.width, self.timeLabel.frame.size.height);

//	NSThread *downloadImageThread = [[NSThread alloc]initWithBlock:^{
//
//	}];
//	downloadImageThread.name = @"downloadImageThread";
//	[downloadImageThread start];
	
//	dispatch_queue_global_t dowloadQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//	dispatch_queue_main_t mainQueue =  dispatch_get_main_queue();
//
//	dispatch_async(dowloadQueue, ^{
//		UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:item.thumbnailPicS]]];
//		dispatch_async(mainQueue, ^{
//			self.rightImageView.image = image;
//		});
//	});
	
//	NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//	NSBlockOperation *operation = [[NSBlockOperation alloc]init];
//	[operation addExecutionBlock:^{
//		UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:item.thumbnailPicS]]];
//		dispatch_async(mainQueue, ^{
//			self.rightImageView.image = image;
//		});
//	}];
//
//	[queue addOperation:operation];
	
	
//    self.rightImageView.image = [UIImage imageNamed:@"icon"];
	
	[self.rightImageView sd_setImageWithURL:[NSURL URLWithString:item.thumbnailPicS] placeholderImage:[UIImage imageNamed:@"icon"]];
}

@end
